import 'dart:convert';

import 'package:app/pages/daily_mission_report_module/my_mission_page.dart';
import 'package:app/utils/hexcolor.dart';
import 'package:app/utils/search_module.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:get/get.dart';
import 'package:image_pickers/image_pickers.dart';

// import 'package:image_pickers/image_pickers.dart';
import '../../common/app_theme.dart';
import '../../common/colors.dart';
import '../../components/bottom_enter_page.dart';
import 'dart:io';

import '../contact_module/contact_active_page.dart';
import '../contact_module/contact_organ_page.dart';
import '../my_project_report_module/my_project_page.dart';
import 'published_mission_controller.dart';
import 'package:bruno/bruno.dart';

class PublishedMissionView extends GetView {
  PublishedMissionController controller = Get.put(PublishedMissionController());

  // BrnHorizontalStepsManager _stepsManager = BrnHorizontalStepsManager();


  


  //确认弹窗
  void confirmDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("温馨提示"),
          content: const Text("您确定要删除当前任务？"),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: const Text("取消"),
            ),
            TextButton(
              child: const Text("确定"),
              onPressed: () async {
                await controller.requestDataDelete();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      // resizeToAvoidBottomInset: false, // 就是这里
      backgroundColor: AppColor.bgColor,
      appBar: AppBar(
        backgroundColor: AppColor.themeColor,
        leading: IconButton(
          onPressed: () {
            Get.back(result: 'refresh');
          },
          icon: Image.asset(
            'images/leftarr.png',
            color: Colors.white,
            width: 15,
            height: 15,
          ),
        ),
        title: Text(controller.task_id > 0 ? '任务详情' : '发布任务'),
        actions: [
          Visibility(
              visible: controller.task_id > 0,
              child: TextButton(
                  onPressed: () {
                    confirmDialog(context);
                  },
                  child: const Icon(Icons.delete_forever_sharp,
                      color: Colors.white, size: 24)))
        ],
      ),
      body: GetBuilder<PublishedMissionController>(builder: (_) {
        return GestureDetector(
          onTap: () {
            SearchModule.closeKeyboard(context);
          },
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },
          child: Column(
            children: [
              Expanded(
                child: ListView(
                  children: [
                    BrnInputText(
                      maxHeight: 200,
                      minHeight: 30,
                      minLines: 1,
                      maxLength: 50,
                      bgColor: Colors.white,
                      textInputAction: TextInputAction.newline,
                      maxHintLines: 20,
                      textEditingController: controller.editingController,
                      hint: '一句话描述任务...',
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      onTextChange: (text) {},
                      onSubmit: (text) {
                        print(text);
                      },
                    ),
                    const SizedBox(
                      height: 1,
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      color: HexColor('#F8F8F8'),
                      width: Get.width - 80,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [Text("基本信息")],
                      ),
                    ),
                    const SizedBox(
                      height: 1,
                    ),
                    GestureDetector(
                        onTap: () {
                          DatePicker.showDatePicker(context,
                              showTitleActions: true,
                              minTime: DateTime(2022, 11, 11),
                              maxTime: DateTime(
                                  controller.endedarray[0],
                                  controller.endedarray[1],
                                  controller.endedarray[2]),
                              onChanged: (date) {}, onConfirm: (date) {
                            int year = date.year;
                            int month = date.month;
                            int day = date.day;
                            String years = '$year';
                            String months = month > 9 ? '$month' : '0$month';
                            String days = day > 9 ? '$day' : '0$day';
                            controller.startarray = [year, month, day];
                            controller.start_time = '$years-$months-$days';
                            controller.refresh();
                          },
                              currentTime: DateTime.now(),
                              locale: LocaleType.zh);
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("起始日"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Text(controller.start_time),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 20,
                    ),
                    GestureDetector(
                        onTap: () async {
                          var data = await Get.to(ContactActivePage(
                              positionids: controller.activearraids));
                          if (data['data'] == 'refresh') {
                            controller.activestrings = "";
                            controller.activenames = data['namess'];
                            controller.activearraids = data['positions'];
                            controller.activenames.forEach((element) {
                              print("element==>>>${element}");
                              controller.activestrings += element;
                              controller.activestrings += ",";
                            });
                            controller.refresh();
                          }
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("参与者"),
                              Container(
                                  child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Container(
                                      alignment: Alignment.topRight,
                                      width: 250,
                                      child: Text(controller.activestrings)),
                                  const Icon(
                                    Icons.arrow_forward_ios,
                                    size: 15,
                                  )
                                ],
                              )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),

                    GestureDetector(
                        onTap: () async {
                          controller.tapClickTaskState();
                        },
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("任务状态"),
                              Container(color: Colors.white,
                                  child: Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceAround,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                          alignment: Alignment.topRight,
                                          width: 200,
                                          child: Text(controller.state)),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),


                 GestureDetector(
                   onTap: (){
                     controller.tapClickTaskState();
                   },
                   child:    Container(
                     child: BrnHorizontalSteps(
                       controller: controller.brnStepsController,
                       steps: [
                         BrunoStep(
                           stepContentText: "进行中",
                         ),
                         BrunoStep(
                           stepContentText: "阻塞中",
                         ),
                         BrunoStep(
                           stepContentText: "已完成",
                         ),
                       ],

                     ),
                   ),
                 ),

                    GestureDetector(
                        onTap: () {
                          DatePicker.showDatePicker(context,
                              showTitleActions: true,
                              minTime: DateTime(
                                  controller.startarray[0],
                                  controller.startarray[1],
                                  controller.startarray[2]),
                              maxTime: DateTime(2030, 12, 12),
                              onChanged: (date) {}, onConfirm: (date) {
                            int year = date.year;
                            int month = date.month;
                            int day = date.day;
                            String years = '$year';
                            String months = month > 9 ? '$month' : '0$month';
                            String days = day > 9 ? '$day' : '0$day';
                            controller.endedarray = [year, month, day];
                            controller.ended_time = '$years-$months-$days';
                            controller.refresh();
                          },
                              currentTime: DateTime.now(),
                              locale: LocaleType.zh);
                        },
                        child: Container(
                          padding: const EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              const Text("到期日"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Text(controller.ended_time),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    Visibility(
                        visible: true,
                        child: GestureDetector(
                            onTap: () async {
                              var data = await Get.to(MyProjectPage(
                                  project_id: controller.project_id));
                              if (data['data'] == 'refresh') {
                                controller.project_id = data['project_id'];
                                controller.project_name = data['project_name'];
                                controller.refresh();
                              }
                            },
                            child: Container(
                              padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                              color: HexColor('#FEFEFE'),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text("关联项目"),
                                  Container(
                                      child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                          margin:
                                              const EdgeInsets.only(right: 12),
                                          alignment: Alignment.topRight,
                                          width: 200,
                                          child: Text(controller.project_name)),
                                      const Icon(
                                        Icons.arrow_forward_ios,
                                        size: 15,
                                      )
                                    ],
                                  )),
                                ],
                              ),
                            ))),
                    const SizedBox(
                      height: 1,
                    ),
                    Visibility(
                        visible: false,
                        child: Container(
                          padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                          color: HexColor('#FEFEFE'),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text("进度"),
                              Container(
                                  height: 40,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      SizedBox(
                                        width: 240,
                                        child: Slider(
                                          min: 0.0,
                                          max: 1.0,
                                          value: controller.intensity,
                                          onChanged: (double svalue) {
                                            controller.refreshSlider((svalue));
                                          },
                                        ),
                                      ),
                                      Container(
                                          width: 40,
                                          child: Text(
                                              "${(controller.intensity * 100).floor()}%")),
                                    ],
                                  )),
                            ],
                          ),
                        )),
                    const SizedBox(
                      height: 1,
                    ),
                    BrnInputText(
                      maxHeight: 200,
                      minHeight: 30,
                      minLines: 1,
                      maxLength: 50,
                      bgColor: Colors.white,
                      textInputAction: TextInputAction.newline,
                      maxHintLines: 20,
                      textEditingController: controller.editingController1,
                      hint: '这里可以添加任务详情',
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      onTextChange: (text) {},
                      onSubmit: (text) {
                        print(text);
                      },
                    ),
                    const SizedBox(
                      height: 1,
                    ),
                  ],
                ),
              ),
              // Expanded(child: BottomEnterPage()),
             Container(
               color: Colors.white,
               child:  BottomEnterPage(() {
                 if (controller.editingController.text.isEmpty) {
                   BotToast.showText(text: '名称不能为空,请填写名称');
                   return;
                 }
                 controller.requestDataWithPublish();
                 print('点击了发布');
               }),
             )
            ],
          ),
        );
      }),
    );
  }
}
